﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Diagnostics;
using System.IO.IsolatedStorage;
using SQLiteClient;

namespace Vanglen.BillBox
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            Common.CopyContentToIsolatedStorage("db/dbbillbox.db");
            Common.CopyContentToIsolatedStorage("db/dbbillbox.db-journal");

            //SQLiteClient.SQLiteConnection db = new SQLiteConnection("dbbillbox.db");
            //db.Open();

            //SQLiteCommand cmd = db.CreateCommand(" CREATE TABLE bills (id INTEGER PRIMARY KEY, name TEXT, items TEXT, cost TEXT, type TEXT, note TEXT, date TEXT, time TEXT); ");
            //cmd.ExecuteNonQuery();

            //db.Dispose();
            //db = null;


        }

        //文本框中只输入数字
        private void PhoneTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            Debug.WriteLine("TelephoneNumber InputScope------PlatformKeyCode:" + e.PlatformKeyCode + "e.key" + e.Key);
            if ((e.PlatformKeyCode == 190) && (sender as TextBox).Text.Contains('.'))
            {
                e.Handled = true;
            }
            else if ((e.PlatformKeyCode != 190) && !(e.PlatformKeyCode >= 0x60 && e.PlatformKeyCode <= 0x69))
            {
                e.Handled = true;
            }
        }

        private void DatePicker_ValueChanged(object sender, DateTimeValueChangedEventArgs e)
        {

        }

        private void TimePicker_ValueChanged(object sender, DateTimeValueChangedEventArgs e)
        {

        }

        private void ApplicationBarIconButtonDetial_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Detail.xaml", UriKind.RelativeOrAbsolute));
        }

        private void ApplicationBarIconButtonSave_Click(object sender, EventArgs e)
        {
            try
            {
                this.Save();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void ApplicationBarIconButtonClear_Click(object sender, EventArgs e)
        {
            try
            {
                this.ClearData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Save()
        {
            Bills b = new Bills();
            b.items = tbItems.SelectedItem.ToString();
            b.cost = tbCost.Text;
            b.date = tbDate.Value.Value.ToString("yyyy-MM-dd");
            b.time = tbTime.Value.Value.ToString("HH:mm:ss");
            b.type = tbType.SelectedItem.ToString();
            b.note = tbNote.Text;

            using (SQLiteHelper helper = new SQLiteHelper())
            {
                string sql = "insert into bills (items, cost, type, date, time, note) values(@items,@cost,@type,@date,@time,@note) ";
                int result = helper.ExecuteNonQuery<Bills>(sql, b);
                if (result > 0)
                    MessageBox.Show("保存成功！");
                ClearData();
            }
        }

        private void ClearData()
        {
            this.tbItems.SelectedIndex = 0;
            this.tbCost.Text = "0";
            this.tbDate.Value = DateTime.Now;
            this.tbTime.Value = DateTime.Now;
            this.tbType.SelectedIndex = 0;
            this.tbNote.Text = "";
            this.tbItems.Focus();
        }

        private void ApplicationBarMenuItem_Click(object sender, EventArgs e)
        {
            CustomMessageBox messageBox = new CustomMessageBox()
            {
                Caption = "关于我们",
                Message = "如果您对本产品有任何的意见或建议，欢迎联系我：Email:omicle@sina.com",
                LeftButtonContent = "OK",
                IsRightButtonEnabled = false,
                IsFullScreen = true
            };
            messageBox.Show();
        }
    }
}